package example.listview;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import example.listview.R;

import java.util.ArrayList;
import java.util.Map;

public class ListViewAdapter extends BaseAdapter {

  public Map<String, String> viewMap;
  public ArrayList<String> keyList;

  Activity activity;

  public ListViewAdapter(Activity activity, Map<String, String> viewMap) {
    super();
    this.activity = activity;
    this.viewMap = viewMap;
    this.keyList = new ArrayList<String>(viewMap.keySet());
  }

  @Override
  public int getCount() {
    return keyList.size();
  }

  @Override
  public Object getItem(int position) {
    return keyList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return 0;
  }


  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = activity.getLayoutInflater();

    if (convertView == null) {
      convertView = inflater.inflate(R.layout.listview_row, null);
    } 

    String key = keyList.get(position);
    ((TextView) convertView.findViewById(R.id.KeyText)).setText(key);
    ((TextView) convertView.findViewById(R.id.ValueText)).setText(viewMap.get(key));

    return convertView;
  }

}

